package com.my.blog.myblog.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.IPage;
import com.my.blog.annotation.SystemLog;
import com.my.blog.myblog.entity.Blackip;
import com.my.blog.myblog.service.BlackIpService;
import com.my.blog.utils.ConstantUtil;
import com.my.blog.utils.ResultBean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/***
 *
 * @Auther: MuYun
 * @Date: 2021/9/17 18:11
 * @Description： 黑名单的controller
 *
 */
@RestController
@RequestMapping("/admin")
public class AdminBlackIpController {

    @Resource
    private BlackIpService blackIpService;


    @PostMapping("/deleteBlackIp")
    @SystemLog(description = ConstantUtil.BACKIP_DELETE,userType = ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> deleteBlackIp(@RequestParam(value = "id") String id){
        ResultBean<Object> resultBean=new ResultBean<>();
        QueryWrapper<Blackip> queryWrapper=new QueryWrapper<>();
        if(blackIpService.remove(queryWrapper.lambda().eq(Blackip::getVid,id))){
            resultBean.setCode(ResultBean.SUCCESS);
        }else {
            resultBean.setCode(ResultBean.NO_PERMISSION);
        }
        return resultBean;
    }

    //黑名单列表查询+模糊
    @PostMapping("/selectLikeBlackIpListByPage")
    public ResultBean<Page<Blackip>> selectLikeBlackIpListByPage(Blackip blackip,
                                      @RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                      @RequestParam(value = "startTime")String startTime,
                                      @RequestParam(value = "endTime")String endTime){
        ResultBean<Page<Blackip>> resultBean=new ResultBean<>();
        QueryWrapper<Blackip> queryWrapper=new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(blackip.getIp())){
            queryWrapper.lambda().like(Blackip::getIp,blackip.getIp());
        }
        if(ObjectUtil.isNotEmpty(blackip.getCity())){
            queryWrapper.lambda().like(Blackip::getCity,blackip.getCity());
        }
        if(ObjectUtil.isNotEmpty(blackip.getBrowserType())){
            queryWrapper.lambda().like(Blackip::getBrowserType,blackip.getBrowserType());
        }
        if(ObjectUtil.isNotEmpty(blackip.getPlatformType())){
            queryWrapper.lambda().like(Blackip::getPlatformType,blackip.getPlatformType());
        }
        if(ObjectUtil.isNotEmpty(startTime)){
            queryWrapper.lambda().gt(Blackip::getTime,startTime);
        }
        if(ObjectUtil.isNotEmpty(endTime)){
            queryWrapper.lambda().lt(Blackip::getTime,endTime);
        }
        Page<Blackip> blackipPage = blackIpService.page(new Page<>(page, pageSize), queryWrapper);
        if(ObjectUtil.isNotEmpty(blackipPage.getRecords())){
            resultBean.setData(blackipPage);
        }else {
            blackipPage.setTotal(0);
            resultBean.setData(blackipPage);
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    //查询黑名单总数
    @PostMapping("/selectAllBlackIpCount")
    public ResultBean<Integer> selectAllBlackIpCount(){
        ResultBean<Integer> resultBean=new ResultBean<>();
        int count = blackIpService.count();
        resultBean.setData(count);
        return resultBean;
    }

    //新增黑名单
    @PostMapping("/addBlackIp")
    @SystemLog(description = ConstantUtil.BACKIP_ADD,userType = ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> addBlackIp(Blackip blackip){
        ResultBean<Object> resultBean=new ResultBean<>();
        QueryWrapper<Blackip> queryWrapper=new QueryWrapper<>();
        List<Blackip> blackipList = blackIpService.list(queryWrapper.lambda().eq(Blackip::getIp, blackip.getIp()));
        //已有该ip
        if(blackipList.size()!=0){
            resultBean.setCode(2);
            return resultBean;
        }else {
            //设置新增时间
            blackip.setTime(LocalDateTime.now());
            //保存
            if(blackIpService.save(blackip)){
                resultBean.setCode(ResultBean.SUCCESS);
            }else {
                resultBean.setCode(ResultBean.FAIL);
            }
        }

        return resultBean;
    }

}
